﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace frmCheckStock
{
    public partial class frmSupplier : Form
    {
        LogicUniversityT2Entities4 context = new LogicUniversityT2Entities4();
        
        int rownum = 0;
        public frmSupplier()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            gbSupplierAddNew.Visible = true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Supplier supplierUpdate = context.Suppliers.First(sup => sup.SupplierID == lblSupplierCode.Text);
            supplierUpdate.ContactName = txtUpdateContactName.Text;
            supplierUpdate.PhoneNo = txtUpdatePhoneNo.Text;
            supplierUpdate.FaxNo = txtUpdateFaxNo.Text;
            supplierUpdate.Address = txtUpdateAddress.Text;
            context.SaveChanges();
            refreshGridView();
            gbSupplierUpdate.Visible = false;
            MessageBox.Show("Supplier info has been successfully updated");

        }

        private void frmSupplier_Load(object sender, EventArgs e)
        {
            
            refreshGridView();

            
 

        }

        private void gvSupplier_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            gbSupplierUpdate.Visible = true;
            gbSupplierAddNew.Visible = false;
            DataGridViewTextBoxCell supName = (DataGridViewTextBoxCell)gvSupplier.Rows[gvSupplier.CurrentRow.Index].Cells[1];
            String supNameSelected = supName.Value.ToString();
            Supplier supplier = context.Suppliers.First<Supplier>(sup => sup.SupplierName == supNameSelected);
            lblRegisNo.Text = supplier.GSTRegistrationNo;
            txtUpdateAddress.Text = supplier.Address;
            txtUpdateContactName.Text = supplier.ContactName;
            txtUpdateFaxNo.Text = supplier.FaxNo;
            txtUpdatePhoneNo.Text = supplier.PhoneNo;
            lblSupplierCode.Text = supplier.SupplierID;
            lblSupplierName.Text = supplier.SupplierName;


        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            Supplier newSupplier = new Supplier();


            newSupplier.GSTRegistrationNo = txtGST.Text;
            newSupplier.SupplierID = txtNewSupplierCode.Text;
            newSupplier.SupplierName = txtNewSupplierName.Text;
            newSupplier.ContactName = txtNewContactName.Text;
            newSupplier.PhoneNo = txtNewPhoneNo.Text;
            newSupplier.FaxNo = txtNewFaxNo.Text;
            newSupplier.Address = txtNewAddress.Text;
            context.AddToSuppliers(newSupplier);
            context.SaveChanges();
            refreshGridView();
            gbSupplierAddNew.Visible = false;
            MessageBox.Show("New Supplier has been successfully added");
        }

        private void gvSupplier_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
        {
            e.ContextMenuStrip = contextMenuStrip1;
            rownum = e.RowIndex;
        }

        private void deleteSupplierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewTextBoxCell supNameDelete = (DataGridViewTextBoxCell)gvSupplier.Rows[gvSupplier.CurrentRow.Index].Cells[1];
            String supNameSelected = supNameDelete.Value.ToString();
            Supplier supplierDelete = context.Suppliers.First(sup => sup.SupplierName == supNameSelected );
            context.Suppliers.DeleteObject(supplierDelete);
            context.SaveChanges();
            MessageBox.Show("Record has been successfully deleted");
            refreshGridView();
        }

        private void refreshGridView()
        {
            var c = from sup in context.Suppliers
                    select new
                    {
                        Name = sup.SupplierName,
                        ContactName = sup.ContactName,
                        PhoneNo = sup.PhoneNo,

                    };
            var supply = c.ToList();
            gvSupplier.DataSource = supply;

            

            for (int i = 0; i < supply.Count; i++)
            {

                gvSupplier[0, i].Value = i + 1;

            }

        }
    }
}
